﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlLocInvenProcess
    {
        /// <summary>
        /// 8.0通过库位号获取库存信息 Add By Quinn.Ling 201703
        /// DataSource:Orders,OrdHistory,OrdHistoryDtl,PickDetail
        /// </summary>
        public static string GetPDALocInvenData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string loc = string.Empty;
            string sku = string.Empty;
            string lpn = string.Empty;
            string lot = string.Empty;
            string qty = string.Empty;
            string return_status = "";
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点

            #endregion

            try
            {
                #region 参数源
                //App.ws = new Fcl.WSOP.sqldbop();
                //App.ws.Init(System.Configuration.ConfigurationManager.ConnectionStrings["adpwmsConnectionString"].ConnectionString);
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNodeList rootlist = root.ChildNodes;
                #endregion

                for (int i = 0; i < rootlist.Count; i++)
                {
                    #region 参数源
                    XmlNode nodeOrder = rootlist[i];
                    try
                    {
                        userid = nodeOrder.SelectSingleNode("userid").InnerText;
                        whseid = nodeOrder.SelectSingleNode("whseid").InnerText;
                        loc = nodeOrder.SelectSingleNode("loc").InnerText;

                        strSql = "SELECT loc,lot,sku,lpn,(qty-allocated_qty-picked_qty) AS qty  FROM Inventory WHERE loc='" + loc+"'";
                        DataTable dt = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                        if (dt.Rows.Count > 0)
                        {
                            for(int j = 0; j < dt.Rows.Count; j++)
                            {
                                sku = dt.Rows[j]["sku"].ToString();
                                lpn = dt.Rows[j]["lpn"].ToString();
                                lot = dt.Rows[j]["lot"].ToString();
                                qty = dt.Rows[j]["qty"].ToString().Substring(0, dt.Rows[j]["qty"].ToString().Length-3);

                                #region 构建XML报文
                                XmlNode order_result = docreturn.CreateElement("inventory_return");

                                XmlElement useridResult = docreturn.CreateElement("userid");
                                useridResult.InnerText = userid;
                                order_result.AppendChild(useridResult);

                                XmlElement whseidResult = docreturn.CreateElement("whseid");
                                whseidResult.InnerText = whseid;
                                order_result.AppendChild(whseidResult);

                                XmlElement locResult = docreturn.CreateElement("loc");
                                locResult.InnerText = loc;
                                order_result.AppendChild(locResult);

                                XmlElement skuResult = docreturn.CreateElement("sku");
                                skuResult.InnerText = sku;
                                order_result.AppendChild(skuResult);

                                XmlElement lpnResult = docreturn.CreateElement("lpn");
                                lpnResult.InnerText = lpn;
                                order_result.AppendChild(lpnResult);

                                XmlElement batchResult = docreturn.CreateElement("batch");
                                batchResult.InnerText = lot;
                                order_result.AppendChild(batchResult);

                                XmlElement qtyResult = docreturn.CreateElement("qty");
                                qtyResult.InnerText = qty;
                                order_result.AppendChild(qtyResult);

                                XmlElement return_statusResult = docreturn.CreateElement("return_status");
                                return_statusResult.InnerText = return_status;
                                order_result.AppendChild(return_statusResult);

                                rootResult.AppendChild(order_result);
                                #endregion
                            }
                        }
                        else
                        {
                            return_status = "库位信息为空";
                        }

                    }
                    catch (Exception e) { }
                    #endregion

                }

            }
            catch (Exception ex)
            {
                App.Log("LocInven", ex.Message.ToString());
                //throw new Exception("异常源：PDAXmlOrderPickProcess:GetPDAOrderPickData;" + "  " + ex.Message);
            }

            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }

    }
}
